package com.minxi.jsp.dao;

import com.minxi.jsp.javabean.Userinfo;
import com.mysql.cj.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class UserDaoImpl implements UserDao {
    @Override
    public Boolean add(Userinfo userinfo) {
        Boolean flag = false;
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/student?serverTimezone=Asia/Shanghai";
            String user = "root";
            String pwd = "123456";
            connection = DriverManager.getConnection(url, user, pwd);
            String sql = "insert into user_info(user_name,user_pwd,user_phone,user_sex) values(?,?,?,?);";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, userinfo.getUserName());
            preparedStatement.setString(2, userinfo.getUserPwd());
            preparedStatement.setString(3, userinfo.getUserPhone());
            preparedStatement.setInt(4, userinfo.getUsersex());
            int i = preparedStatement.executeUpdate();
            if (i > 0) {
                flag = true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                connection.close();
                preparedStatement.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return flag;
    }
}
